{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "initial_id",
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "'''\n",
    "比如下图是一篇英文短文，我们想做文本分类，用某些算法去做。算法的内部是一些数学公式运算之类的，数学公式能够处理字符串吗？是不能的，这个时候需要通过一些方法，将文本字符串之类的转换成数值类型。但是怎么转换比较合适，怎么转换才能达到我们的目的呢，这就是特征提取要考虑的事情。\n",
    "\n",
    "再比如泰坦尼克号的数据集中的性别，还是存储成了字符串male和femal，表示类别。同样要用机器学习算法去处理之前还是要将类型转换成数值（ont-hot编码或者哑变量）。                     \n",
    "原文链接：https://blog.csdn.net/qq_27328197/article/details/113807051\n",
    "所以特征提取可以这样说：特征提取是将任意数据（如文本或者图像）转换为机器学习的数字特征（特征值化是为了让计算机更好的去理解数据），包括字典特征抽取、文本特征提取、图像特征提取（深度学习）等。\n",
    "'''"
   ]
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "'''\n",
    "DictVectorizer()使用默认参数会返回一个稀疏矩阵\n",
    "'''"
   ],
   "id": "cc7434988933185c"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-09T11:24:48.363524Z",
     "start_time": "2025-01-09T11:24:46.488845Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.feature_extraction import DictVectorizer\n",
    "from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer\n",
    "from sklearn.preprocessing import MinMaxScaler, StandardScaler\n",
    "from sklearn.feature_selection import VarianceThreshold\n",
    "from sklearn.decomposition import PCA\n",
    "import jieba\n",
    "import numpy as np\n",
    "from sklearn.impute import SimpleImputer"
   ],
   "id": "8c7c424b3481dca",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-09T11:37:26.866894Z",
     "start_time": "2025-01-09T11:37:26.861733Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def dictvec():\n",
    "    dict1 = DictVectorizer(sparse = True)\n",
    "    data = dict1.fit_transform([{'city': '北京', 'temperature': 100},\n",
    "                               {'city': '上海', 'temperature': 60},\n",
    "                               {'city': '深圳', 'temperature': 30}])\n",
    "    print(data)\n",
    "    print('-' * 50)\n",
    "    # 字典中的一些类别数据，分别进行转换成特征\n",
    "    print(dict1.get_feature_names_out())\n",
    "    print('-' * 50)\n",
    "    print(dict1.inverse_transform(data))  #去看每个特征代表的含义，逆转回去\n",
    "\n",
    "    return None\n",
    "\n",
    "dictvec()\n",
    "    "
   ],
   "id": "cfd1305b4eb58191",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<Compressed Sparse Row sparse matrix of dtype 'float64'\n",
      "\twith 6 stored elements and shape (3, 4)>\n",
      "  Coords\tValues\n",
      "  (0, 1)\t1.0\n",
      "  (0, 3)\t100.0\n",
      "  (1, 0)\t1.0\n",
      "  (1, 3)\t60.0\n",
      "  (2, 2)\t1.0\n",
      "  (2, 3)\t30.0\n",
      "--------------------------------------------------\n",
      "['city=上海' 'city=北京' 'city=深圳' 'temperature']\n",
      "--------------------------------------------------\n",
      "[{'city=北京': np.float64(1.0), 'temperature': np.float64(100.0)}, {'city=上海': np.float64(1.0), 'temperature': np.float64(60.0)}, {'city=深圳': np.float64(1.0), 'temperature': np.float64(30.0)}]\n"
     ]
    }
   ],
   "execution_count": 7
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
